var app = sm("do_App");
var page = sm("do_Page");
var openHelper = require("helper/open");
var nf = deviceone.sm("do_Notification");
var httpSilence = require("helper/httpSilence");
var mallUrl = require("url/mall");
var root=ui("$");
var userUtil = require("helper/user");
//设置数据绑定的映射关系
root.setMapping({
	"do_LinearLayout_root.tag":"userId",
	"ALayout_aiHao.tag":"interests",
	"do_ALayout_Industry.tag":"industry",//行业
	"do_ALayout_HomeTown.tag":"hometown",//家乡
	"do_ALayout_Delicacy.tag":"delicacy",//美食
	"do_ALayout_GraduateSchool.tag":"graduateSchool",//学校
	"do_ALayout_Entertainment.tag":"entertainment",//娱乐
	"ALayout_community.tag":"communityList",
	"gridViewCircle.items":"circleList",
	
	"ALayout_aiHao.visible": "showInrerests",//显示爱好
	"do_ALayout_Industry.visible":"showIndustry",//显示行业
	"do_ALayout_HomeTown.visible":"showHomeTown",//显示家乡
	"do_ALayout_Delicacy.visible":"showDelicacy",//显示美食
	"do_ALayout_GraduateSchool.visible":"showGraduateSchool",//显示学校
	"do_ALayout_Entertainment.visible":"showEntertainment",//显示娱乐
	"ALayout_community.visible" : "showCommunity",//显不社区layout
	"lableJoinCommunityT.visible": "showJoinCommunity",//显示社区label
	"labelCommunityCount.visible": "showJoinCommunityCount",//显示社区数量
	"lableJoinCommunityT2.visible": "showJoinCommunity2",//显示社区label【长】
	"aLayoutCircle.visible": "showCircle",//显示圈子
	"do_ALayout_shop.visible": "showShop",//显示店铺
	"do_ALayout_herCircle.visible": "showHerCircle", //显示-他的圈子label
	"do_ALayout_herCircle.tag": "sex" //性别
});
function getNameById(id,arrayData)
{
  for(var i=0;i<arrayData.length;i++)
	  {
	  if(arrayData[i]["id"] == id)
		  return arrayData[i]['name'];	  
	  }
}
var sexName = "她";
root.on("dataRefreshed", function(){
	var sex = ui("do_ALayout_herCircle").tag;
	sexName = (sex == "1") ? "他":"她";
	ui("labelHerCircleT").text = sexName+"的圈子";
	ui("lableJoinCommunityT2").text = sexName+"加入的社区";
	ui("labelShop").text = sexName+"的店铺";
	
	reflashInterest();
	reflashIndustry();
	reflashHomeTown();
	reflashDelicacy();
	reflashGraduateSchool();
	reflashEntertainment();
	reflashCommunity();
	setCircleCount();
});

function setCircleCount() {
	var circleList = JSON.parse(ui("gridViewCircle").items);
	if(circleList.length>0){
		ui("labelCircleCount").text = circleList.length;//圈子数量
		ui("labelHerCircle").text = circleList.length;//他的圈子数量
	}
}
//兴趣
var listdata4Interests = mm("do_ListData");
var segmentView4Interest = ui("segmentViewInterest");
segmentView4Interest.bindItems(listdata4Interests);
function reflashInterest(){
	var interests = ui("ALayout_aiHao").tag;
	if(null!=interests&&""!=interests){
		listdata4Interests.removeAll();
		var interestList = interests.split(",");
		var jsonImgs = [];
		if(interestList.length>0){
			ui("labelInterestCount").text = interestList.length;//爱好数量
		}
		for (var i = 0; i < interestList.length; i++) {
			jsonImgs.push({
				interest : interestList[i]
			});
		}
		listdata4Interests.addData(jsonImgs);
		segmentView4Interest.refreshItems();
	}
}
//行业
var listdata4Industry = mm("do_ListData");
var segmentView4Industry = ui("segmentViewIndustry");
segmentView4Industry.bindItems(listdata4Industry);
var industryDatas = JSON.parse(userUtil.loadUserPara(1,'industry'));
for(var i=0;i<industryDatas.length;i++)
{
	industryDatas[i]['id'] = industryDatas[i]['val'];
}
function reflashIndustry(){
	var industry = ui("do_ALayout_Industry").tag;
	if(null!=industry&&""!=industry){
		listdata4Industry.removeAll();
		var industryList = industry.split(",");
		var jsonImgs = [];

		for (var i = 0; i < industryList.length; i++) {
			jsonImgs.push({
				interest : getNameById(industryList[i],industryDatas)
			});
		}
		listdata4Industry.addData(jsonImgs);
		segmentView4Industry.refreshItems();
	}
}
//故乡
var hometownDatas = JSON.parse(userUtil.loadUserPara(1,'hometown'));
for(var i=0;i<hometownDatas.length;i++)
{
	hometownDatas[i]['id'] = hometownDatas[i]['val'];
}
var listdata4HomeTown = mm("do_ListData");
var segmentView4HomeTown = ui("segmentViewHomeTown");
segmentView4HomeTown.bindItems(listdata4HomeTown);
function reflashHomeTown(){
	var hometown = ui("do_ALayout_HomeTown").tag;
	if(null!=hometown&&""!=hometown){
		listdata4HomeTown.removeAll();
		var hometownList = hometown.split(",");
		var jsonImgs = [];

		for (var i = 0; i < hometownList.length; i++) {
			jsonImgs.push({
				interest : getNameById(hometownList[i],hometownDatas)
			});
		}
		listdata4HomeTown.addData(jsonImgs);
		segmentView4HomeTown.refreshItems();
	}
}
//美食
var listdata4Delicacy = mm("do_ListData");
var segmentView4Delicacy = ui("segmentViewDelicacy");
segmentView4Delicacy.bindItems(listdata4Delicacy);
function reflashDelicacy(){
	var delicacy = ui("do_ALayout_Delicacy").tag;
	if(null!=delicacy&&""!=delicacy){
		listdata4Delicacy.removeAll();
		var delicacyList = delicacy.split(",");
		var jsonImgs = [];
		if(delicacyList.length>0){
			ui("labelDelicacyCount").text = delicacyList.length;//爱好数量
		}
		for (var i = 0; i < delicacyList.length; i++) {
			jsonImgs.push({
				interest : delicacyList[i]
			});
		}
		listdata4Delicacy.addData(jsonImgs);
		segmentView4Delicacy.refreshItems();
	}
}
//毕业学校
var listdata4graduateSchool = mm("do_ListData");
var segmentView4GraduateSchool = ui("segmentViewGraduateSchool");
segmentView4GraduateSchool.bindItems(listdata4graduateSchool);
function reflashGraduateSchool(){
	var graduateSchool = ui("do_ALayout_GraduateSchool").tag;
	if(null!=graduateSchool&&""!=graduateSchool){
		listdata4graduateSchool.removeAll();
		var graduateSchoolList = graduateSchool.split(",");
		var jsonImgs = [];

		for (var i = 0; i < graduateSchoolList.length; i++) {
			jsonImgs.push({
				interest : graduateSchoolList[i]
			});
		}
		listdata4graduateSchool.addData(jsonImgs);
		segmentView4GraduateSchool.refreshItems();
	}
}
//娱乐
var listdata4Entertainment = mm("do_ListData");
var segmentView4Entertainment = ui("segmentViewEntertainment");
segmentView4Entertainment.bindItems(listdata4Entertainment);
function reflashEntertainment(){
	var entertainment = ui("do_ALayout_Entertainment").tag;
	if(null!=entertainment&&""!=entertainment){
		listdata4Entertainment.removeAll();
		var entertainmentList = entertainment.split(",");
		var jsonImgs = [];
		if(entertainmentList.length>0){
			ui("labelEntertainmentCount").text = entertainmentList.length;//爱好数量
		}
		for (var i = 0; i < entertainmentList.length; i++) {
			jsonImgs.push({
				interest : entertainmentList[i]
			});
		}
		listdata4Entertainment.addData(jsonImgs);
		segmentView4Entertainment.refreshItems();
	}
}
//社区
var listdata4Community = mm("do_ListData");
var segmentViewCommunity = ui("segmentViewCommunity");
segmentViewCommunity.bindItems(listdata4Community);

function reflashCommunity(){
	var communityList = JSON.parse(ui("ALayout_community").tag);
	//deviceone.print(JSON.stringify(communityList),"communityList");
	if(communityList.length>0){
		ui("labelCommunityCount").text = communityList.length;//社区数量
	}
	if(null!=communityList && communityList.length>0){
		listdata4Community.removeAll();
		listdata4Community.addData(communityList);
		segmentViewCommunity.refreshItems();
	}
}

//点击【他的店铺】
ui("do_ALayout_shop").on("touch", null, 3000, function(data, e) {
	var userId = ui("do_LinearLayout_root").tag;
	httpSilence.post(mallUrl.url.shop.herShopId,{id:userId},function(data){
		if(data.hasShop == 0){
			nf.toast(sexName+"还没有开店!");
		} else {
			var shopId = data.shopId;
			if(shopId){
				openHelper.open("source://view/qshop/detail/main.ui",{"shopId":shopId});
			}
		}
	});
})

//点击【他的圈子】
ui("do_ALayout_herCircle").on("touch", null, 3000, function(data, e) {
	var userId = ui("do_LinearLayout_root").tag;
	var herCircleCount = parseInt(ui("labelHerCircle").text);
	var title = ui("labelHerCircleT").text;
	if(userId && herCircleCount > 0){
		openHelper.open("source://view/user/usercenter/circle/herCircle/herCircle.ui",{"userId":userId,"title":title});
	}
})